import {
	defineStore
} from 'pinia';
import {
	ref
} from 'vue';
import {
	formatDate,
	GetFluency,
	mergeMove,
} from '../utils/common.js';
export const useTimeStore = defineStore('time', () => {
	//当前阶段, 1=打乱, 2=观察, 3=复原, 4=完成
	const stage = ref(0);
	//当前打乱公式
	const scramble = ref('');
	//打乱开始时间
	const scrambleStartTime = ref(0);
	//打乱结束时间
	const scrambleEndTime = ref(0);
	//打乱用时
	const scrambleTime = ref(0);
	//观察开始时间
	const watchStartTime = ref(0);
	//观察结束时间
	const watchEndTime = ref(0);
	//观察用时
	const watchTime = ref(0);
	//复原开始时间
	const gameStartTime = ref(0);
	//复原结束时间
	const gameEndTime = ref(0);
	//复原用时
	const gameTime = ref(0);
	//复原开始序号
	const gameStartIndex = ref(-1);
	//复原结束序号
	const gameEndIndex = ref(-1);
	//复原步骤
	const gameMoves = ref([]);
	//DNF
	const isGiveUp = ref(false);
	//服务器开始时间
	const serverStartTime = ref(0);
	//服务器结束时间
	const serverEndTime = ref(0);
	// 开始时间字符串
	const playStartTime = ref('');
	// 结束时间字符串
	const playEndTime = ref('');
	//结算数据
	const tps = ref(0.0);
	const turn = ref(0);
	const smooth = ref(0);
	const result = ref([]);
	
	
	const resultIntervalTime = ref(0);
	const resultStampTime = ref(0);
	// 历史记录
	const giveUp = () => {
		isGiveUp.value = true;
	};
	const reset = () => {
		stage.value = 0;
		scramble.value = '';
		scrambleStartTime.value = 0;
		scrambleEndTime.value = 0;
		scrambleTime.value = 0;
		watchStartTime.value = 0;
		watchEndTime.value = 0;
		watchTime.value = 0;
		gameStartTime.value = 0;
		gameEndTime.value = 0;
		gameTime.value = 0;
		gameStartIndex.value = -1;
		gameEndIndex.value = -1;
		isGiveUp.value = false;
		serverStartTime.value = 0;
		serverEndTime.value = 0;
		playStartTime.value = '';
		playEndTime.value = '';
	};
	const startScramble = (str, stime = 0) => {
		stage.value = 1;
		scramble.value = str;
		scrambleStartTime.value = getTime();
		playStartTime.value = formatDate(getTime());
		if (stime > 0) {
			serverStartTime.value = stime;
		}
	};
	const endScramble = () => {
		stage.value = 2;
		scrambleEndTime.value = getTime();
		scrambleTime.value = scrambleEndTime.value - scrambleStartTime.value;
		watchStartTime.value = getTime();
	};
	const endWatch = (idx) => {
		stage.value = 3;
		watchEndTime.value = getTime();
		watchTime.value = watchEndTime.value - watchStartTime.value;
		gameStartTime.value = getTime();
		gameStartIndex.value = idx;
	};
	const endGame = (idx) => {
		stage.value = 4;
		gameEndTime.value = getTime();
		playStartTime.value = formatDate(getTime());
		gameTime.value = gameEndTime.value - gameStartTime.value;
		gameEndIndex.value = idx;
		serverEndTime.value = serverStartTime.value + (gameTime.value * 1000);
	};
	const getTime = () => {
		return Date.now();
	};
	
	const checkout = (allMoveList, allTimeList, model) => {
		let moveList = allMoveList.slice(gameStartIndex.value, gameEndIndex.value);
		let timeList = allTimeList.slice(gameStartIndex.value, gameEndIndex.value);
		
		let review = [];
		let lReduction = [];
		let intervalTime = 0;
		let stampTime = 0;
		stampTime = timeList[timeList.length-1] - timeList[0]
	
		for (let i = 0; i < moveList.length; i++) {
			let move = moveList[i];
			lReduction.push(move);
			let obj = {
				iStep: i + 1,
				sMove: move,
				iInterval: timeList[i],
			};
			
			if(i>0 && timeList[i]){
				intervalTime += timeList[i]
			}
		
			
			review.push(obj);
		}
		let sReduction = mergeMove(lReduction);
		let sReductionArr = sReduction.split(' ');
		// console.log(JSON.stringify(review));
		// console.log(JSON.stringify(lReduction));
		// console.log(sReduction);
		tps.value = Number((sReductionArr.length / (gameTime.value / 1000)).toFixed(1));
		turn.value = sReductionArr.length;
		smooth.value = GetFluency(review);
		resultIntervalTime.value = intervalTime;
		resultStampTime.value = stampTime;
		
		result.value.unshift(JSON.parse(JSON.stringify(
			{
				"mRecord": {
					"iSession": result.value.length + 1,
					"iStartTime": serverStartTime.value > 0 ? serverStartTime.value : Math.ceil(scrambleStartTime.value /
						1000),
					"iEndTime": gameEndTime.value,
					"iReductionMTime": Math.ceil(gameStartTime.value / 1000),
					"iResult": isGiveUp.value ? 2 : 0,
					"mData": {
						"sCubeModel": model,
						"iDuration": gameTime.value,
						"iTurn": turn.value,
						"iRotate": 0,
						"iSmoothIndex": smooth.value,
						"sScramble": scramble.value,
						"sReduction": sReduction,
						"sTPS": tps.value,
						"mReview": {
							// "mUserMoveL": review,
							"mUserMoveL": '',
							"mMoveL": review,
							"mSolutionL": []
						}
					},
					"iExceptionType": 0,
					"iReviewID": 0
				},
				"iLeagueId": 0
			}
		)));
		console.log(JSON.stringify(result.value));
	};
	
	return {
		stage,
		scramble,
		serverStartTime,
		serverEndTime,
		scrambleStartTime,
		scrambleEndTime,
		scrambleTime,
		watchStartTime,
		watchEndTime,
		watchTime,
		gameStartTime,
		gameEndTime,
		gameTime,
		gameStartIndex,
		gameEndIndex,
		gameMoves,
		isGiveUp,
		reset,
		startScramble,
		endScramble,
		endWatch,
		endGame,
		getTime,
		giveUp,
		checkout,
		tps,
		turn,
		smooth,
		result,
		playStartTime,
		playEndTime,
		resultIntervalTime,
		resultStampTime,
	};
});